/*
  * Mirante Tecnologia
  * Copyright 2010, Mirante Informatica LTDA, 
  * and individual contributors as indicated by the @authors tag
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
package com.miranteinfo.seam.hibernate.envers;

import java.lang.reflect.Field;
import java.util.Properties;

import org.hibernate.impl.SessionFactoryImpl;


/**
 * Supports to envers.
 * 
 * @author lucas lins
 *
 */
public class EnversConfiguration {
	
	// ATTRIBUTES ***************************
	
	private static final String AUDIT_TABLE_PREFIX = "org.hibernate.envers.audit_table_prefix";
	private static final String AUDIT_TABLE_SUFFIX = "org.hibernate.envers.audit_table_suffix";
	private static final String DEFAULT_AUDIT_TABLE_SUFFIX = "_AUD";
	
	private String auditTablePrefix;
	private String auditTableSuffix;
	
	// CONSTRUCTOR ***********************
	
	public EnversConfiguration(SessionFactoryImpl sessionFactory){
		init(sessionFactory);
	}
	
	// PUBLIC METHODS ***********************
	
	/**
	 * Verify if entity is an envers entity.
	 * @param entityName
	 * @return boolean
	 */
	public boolean isAuditedEntity(String entityName){
		if(entityName == null) return false;
		if(auditTablePrefix == null){
			return entityName.endsWith(auditTableSuffix);
		}else{
			return entityName.startsWith(auditTablePrefix) && entityName.endsWith(auditTableSuffix);
		}
	}
	
	// PRIVATE METHODS ******************
	
	/**
	 * Initialize configurations.
	 */
	private void init(SessionFactoryImpl sessionFactory){
		try{
			Field propsField = SessionFactoryImpl.class.getDeclaredField("properties");
			propsField.setAccessible(true);
			Properties props = (Properties)propsField.get(sessionFactory);
			
			this.auditTablePrefix = initParam(props.getProperty(EnversConfiguration.AUDIT_TABLE_PREFIX));
			this.auditTableSuffix = initParam(props.getProperty(EnversConfiguration.AUDIT_TABLE_SUFFIX));
			
			if(this.auditTableSuffix == null){
				this.auditTableSuffix = DEFAULT_AUDIT_TABLE_SUFFIX;
			}
			
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * Initialize param value.
	 */
	private String initParam(String param){
		if(param != null){
			if(param.trim().length() < 1){
				return null;
			}else{
				return param.trim();
			}
		}
		return null;
	}
}
